package copy;

import java.net.InetAddress;
import java.util.UUID;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.sys.pojo.PermissionInfoVO;

public class MinaClientHandler extends IoHandlerAdapter
{
	@Override
	public void messageReceived( IoSession session, Object message ) throws Exception
	{
		String s = ( String ) message;
		System.out.println( "来自服务器的消息:" + s );
		// session.write(s);
	}

	@Override
	public void sessionClosed( IoSession session ) throws Exception
	{
		// System.out.println("服务器走了~");
	}

	@Override
	public void sessionCreated( IoSession session ) throws Exception
	{
		System.out.println( "one Client Connection" + session.getRemoteAddress() );
		// session.write( "我来了······" );
	}

	@Override
	public void sessionOpened( IoSession session ) throws Exception
	{
		super.sessionOpened( session );
//		Operator info = new Operator();
//		info.setOperatorAccount( "sls2012" );
//		info.setPwd( "444444" );
//		info.setRoleId( "febae9cc-a7ba-4b95-946f-b1a500f7d7a6" );
		//info.setType( "4" );
//		info.setState( "3" );
		//info.setEmail( "sls4444@sina.com" );
		//info.setTel( "444444444" );
		//info.setServiceCode( "1204,1205,1206" );
		
//		RoleInfo info = new RoleInfo();
//		info.setName( "员工" );
//		info.setRemark( "员工级别" );
		
		PermissionInfoVO info = new PermissionInfoVO();
		//info.setServiceCode( "1299" );
//		info.setName( "login" );
//		info.setpServiceCode( "1101" );
//		info.setLevel( 1 );
//		info.setSort( 2 );
//		info.setMenu( true );
//		info.setUrl( "permission!add.htm" );
		
		SocketDTO socketChangeVo = new SocketDTO();
		
		Gson gson = CommonUtil.gson();
		socketChangeVo.setBody( gson.toJson( info ) );
		Head head = new Head();
		InetAddress addr = InetAddress.getLocalHost();
	    String ip=addr.getHostAddress();//获得本机IP
		head.setIp( ip );
		head.setServiceCode( "1302" );
		head.setUserId( "ming" );
		head.setRequestId( UUID.randomUUID().toString() );
		socketChangeVo.setHead( head );
		String cardMsg = gson.toJson( socketChangeVo );
		session.write( cardMsg );
		System.out.println( "发送"+cardMsg );
		System.out.println( "sesson open!!" );
		// session.close( true );
	}
}
